<template>
  <div class="app">
    <el-dialog :title="title" :visible="showDialog" @close="onCancel">
      <el-form ref="fromref" label-width="100px" :model="formData" :rules="rules">
        <el-form-item label="部门名称" prop="name">
          <el-input v-model="formData.name" placeholder="2-10个字符" />
        </el-form-item>
        <el-form-item label="部门编码" prop="code">
          <el-input v-model="formData.code" placeholder="2-10个字符" />
        </el-form-item>
        <el-form-item label="部门负责人" prop="managerId">
          <el-select v-model="formData.managerId" placeholder="请选择负责人" style="width: 100%;">
            <el-option v-for="item in managerList" :key="item.id" :label="item.username" :value="item.id" />
          </el-select>
        </el-form-item>
        <el-form-item label="部门介绍" prop="introduce">
          <el-input v-model="formData.introduce" type="textarea" placeholder="2-10个字符" :rows="4" />
        </el-form-item>
        <el-form-item style="text-align: center;">
          <el-button type="primary" @click="onsubmit">确认</el-button>
          <el-button @click="onCancel">取消</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>
<script>
import { addDepartmentApi, getDepartmentApi, getDepartmentDetailApi, getManagerListApi, updateDepartmentApi } from '@/api/department'

export default {
  name: 'App',
  props: {
    showDialog: {
      type: Boolean,
      required: true
    },
    pid: {
      type: Number,
      default: null
    },
    title: {
      type: String,
      required: true
    }
  },
  data() {
    return {
      options: [{
        value: '选项1',
        label: '黄金糕'
      }, {
        value: '选项2',
        label: '双皮奶'
      }, {
        value: '选项3',
        label: '蚵仔煎'
      }, {
        value: '选项4',
        label: '龙须面'
      }, {
        value: '选项5',
        label: '北京烤鸭'
      }],
      formData: {
        code: '', // 部门编码
        introduce: '', // 部门介绍
        managerId: '', // 部门负责人id
        name: '', // 部门名称
        pid: '' // 父级部门的id
      },
      rules: {
        code: [{ required: true, message: '部门编码不能为空', trigger: 'blur' },
          {
            min: 2, max: 10, message: '部门编码的长度为2-10个字符', trigger: 'blur'
          },
          {
            validator: async(rules, value, callback) => {
              let depts = await getDepartmentApi()
              if (this.formData.id) {
                depts = depts.filter(item => item.id !== this.formData.id)
              }
              if (depts.some(item => item.code === value)) {
                callback(new Error('部门中已经有该编码了'))
              } else {
                callback()
              }
            },
            trigger: 'blur'
          }
        ], // 部门编码
        introduce: [{ required: true, message: '部门介绍不能为空', trigger: 'blur' }, {
          min: 1, max: 100, message: '部门介绍的长度为1-100个字符', trigger: 'blur'
        }], // 部门介绍
        managerId: [{ required: true, message: '部门负责人不能为空', trigger: 'blur' }], // 部门负责人id
        name: [{ required: true, message: '部门名称不能为空', trigger: 'blur' },
          {
            min: 2, max: 10, message: '部门名称的长度为2-10个字符', trigger: 'blur'
          },
          {
            validator: async(rules, value, callback) => {
              let depts = await getDepartmentApi()
              if (this.formData.id) {
                depts = depts.filter(item => item.id !== this.formData.id)
                console.log(depts)
              }
              if (depts.some(item => item.name === value)) {
                callback(new Error('已经有该部门了'))
              } else {
                callback()
              }
            },
            trigger: 'blur'
          }] // 部门名称
        // pid: '' // 父级部门的id 不需要做校验
      },
      managerList: []
    }
  },
  created() {
    this.getManagerList()
  },
  methods: {
    onCancel() {
      this.formData = {
        code: '', // 部门编码
        introduce: '', // 部门介绍
        managerId: '', // 部门负责人id
        name: '', // 部门名称
        pid: '' // 父级部门的id
      }
      this.$refs.fromref.resetFields()
      this.$emit('update:showDialog', false)
    },
    async onsubmit() {
      await this.$refs.fromref.validate()
      if (this.formData.id) {
        await updateDepartmentApi(this.formData)
      } else {
        await addDepartmentApi({ ...this.formData, pid: this.pid })
      }
      this.$message.success(`${this.formData.id ? '编辑' : '新增'}部门成功`)
      this.$emit('updateDepts')
      this.onCancel()
    },
    async getManagerList() {
      this.managerList = await getManagerListApi()
    },
    async getDepartmentDetail(id) {
      const res = await getDepartmentDetailApi(id)
      this.formData = res
    }
  }
}
</script>
 <style scoped>
</style>
